<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use App\Models\Admin\Category;
use App\Models\Admin\Content;
use Illuminate\Http\Request;

class ContentController extends Controller
{
    //内容首页
    public function index($cid=0){

        // 查询树形分类信息
        $category = (new CategoryController)->getTreeList();
        if($cid==0){
            // 查询所有内容
            $content = Content::get();
        }else{
            $content = Content::where('cid',$cid)->get(); 
        }
        return view('admin.content.index')->with('category',$category)->with('content',$content)->with('cid',$cid); 
        
    }

    //添加内容功能页面
    public function add(){
        // 查询一级栏目
        $category = Category::where('pid',0)->get();
        return view('admin.content.add', ['category' => $category]);
    }

    //保存内容功能
    public function save(Request $request){
        $data = $request->all();
        //参数校验
        $rule = [
            'cid'=>'required',
            'title'=>'required'
        ];
        $message = [
            'cid.required'=>'栏目不能为空',
            'title.required'=>'标题不能为空'
        ];
        $request->validate($rule,$message);

        // 保存数据
        $rs = Content::create($data);
        if($rs){
            return redirect('/content')->with('message','添加成功');
        }else{
            return redirect('/content/add')->with('tip','添加失败');
        }
    }

    public function edit($id)
    {
        $content = Content::findOrFail($id);
        $category = Category::where('pid', 0)->get();
        return view('admin.content.edit', compact('content', 'category'));
    }

    // 更新内容功能
    public function update(Request $request, $id)
    {
        $data = $request->all();
        // 参数校验
        $rule = [
            'cid' => 'required',
            'title' => 'required'
        ];
        $message = [
            'cid.required' => '栏目不能为空',
            'title.required' => '标题不能为空'
        ];
        $request->validate($rule, $message);

        // 更新数据
        $content = Content::findOrFail($id);
        $content->update($data);
        if ($content) {
            return redirect('/content')->with('message', '更新成功');
        } else {
            return redirect("/content/edit/{$id}")->with('tip', '更新失败');
        }
    }

    // 删除内容功能
    public function delete($id)
    {
        $content = Content::findOrFail($id);
        if ($content->delete()) {
            return response()->json(['code' => 1, 'message' => '删除成功']);
        } else {
            return response()->json(['code' => 0, 'message' => '删除失败']);
        }
    }


    // 上传图片
    public function upload(Request $request){
        if($request->hasFile('image')){
            $image = $request->file('image');
            if($image->isValid()){

                // 随机生成文件名
                $name = md5(microtime(true)).'.'.$image->extension();
                $image->move('static/upload',$name);
                $returnData = [
                    'filename'=>$name,
                    'path'=>'/static/upload/'.$name
                ];
                $result = [
                    'code'=>1,
                    'message'=>'上传成功',
                    'time'=>time(),
                    'data'=>$returnData
                ];
                return response()->json($result);
            }else{
                return $image->getErrorMessage();
            }
        }
    }
}
